#include "USlider.h"

#define TWidth 10

TSlider::TSlider(TRectangle rect, TDirection direction)
: TControl(rect)
{
	Pressed = false;
	Direction = direction;

	MaxPosition = 100;
	Position = 50;
}

int TSlider::SliderPos()
{
	return (Right-Left-TWidth)*(Position/float(MaxPosition));
}

void TSlider::Draw()
{
	glColor3f(0.4, 0.4, 0.4);

	glBegin(GL_QUADS);
		glVertex2i(Left		, Top);
		glVertex2i(Right	, Top);
		glVertex2i(Right	, Bottom);
		glVertex2i(Left		, Bottom);
	glEnd();


	float Pos = SliderPos();
	
	glColor3f(1,1,1);
	glBegin(GL_QUADS);
		glVertex2i(Pos			, Top);
		glVertex2i(Pos+TWidth	, Top);
		glVertex2i(Pos+TWidth	, Bottom);
		glVertex2i(Pos			, Bottom);
	glEnd();
}

void TSlider::MouseDown(int X, int Y, uint8 button)
{
	TControl::MouseDown(X, Y, button);
	int Pos = SliderPos();
	if (X>=Pos && X<=Pos+TWidth)
		Pressed = true;
}

void TSlider::MouseUp(int X, int Y, uint8 button)
{
	TControl::MouseUp(X, Y, button);
	Pressed = false;
	LastClickWasOurs = false;
}

void TSlider::MouseLeave()
{
    TControl::MouseLeave();
    Pressed = false;
}

void TSlider::MouseMove(int X, int Y, int mstate)
{
    TControl::MouseMove(X, Y, mstate);
    if (LastClickWasOurs)
	{
		Position = ((X-Left)/float((Right-Left)))*MaxPosition;
        Pressed = true;
	}
}
